package com.study.ssm.annotation;

import com.study.ssm.annotation.bean.User;
import com.study.ssm.annotation.injection.Category;
import com.study.ssm.annotation.injection.UserService;
import com.study.ssm.annotation.lazy.Account;
import com.study.ssm.annotation.scope.Customer;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @author zhoujunlin
 * @date 2023年07月26日 10:44
 * @desc xml+注解bean的方式
 * 去除了xml中的bean相关配置
 */
public class TestAnnotation {

    ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("/annotation/applicationContext01.xml");

    @Test
    public void test1() {
        /*xml覆盖注入*/
        //Overriding bean definition for bean 'u' with a different definition: replacing [Generic bean: class [com.study.ssm.annotation.bean.User]; scope=singleton; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in file [D:\gitee\ssm-code\target\classes\com\study\ssm\annotation\bean\User.class]] with [Generic bean: class [com.study.ssm.annotation.bean.User]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in class path resource [annotation/applicationContext01.xml]]
        User u = ctx.getBean("u", User.class);
        System.out.println(u);
    }

    @Test
    public void test2() {
        Customer customer = ctx.getBean("customer", Customer.class);
        Customer customer1 = ctx.getBean("customer", Customer.class);
        System.out.println(customer);
        System.out.println(customer1);
    }

    @Test
    public void test3() {
        System.out.println("===========");
        // 这个时候实例化
        // Creating shared instance of singleton bean 'account'
        Account account = ctx.getBean("account", Account.class);
        System.out.println(account);
    }

    @Test
    public void test4() {
        // 测试Product对象生命周期
        ctx.close();
    }

    @Test
    public void test5() {
        // 测试注入
        UserService userService = ctx.getBean("userServiceImpl", UserService.class);
        userService.register();
    }

    @Test
    public void test6() {
        /*加载properties并注入属性*/
        Category category = ctx.getBean("category", Category.class);
        System.out.println(category);
    }

    @Test
    public void test7() {
        // 测试Component 包含与排除逻辑
        for (String beanDefinitionName : ctx.getBeanDefinitionNames()) {
            System.out.println("======>" + beanDefinitionName);
        }
    }

}
